package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.FurloughBean;
import com.attendance.www.hr.entitys.Furlough;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IFurloughService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 休假设置
 * 
 * @version 2017/10/10-2017/10/10
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/furlough")
public class FurloughController {

	@Resource
	private IFurloughService furloughService;

	/**
	 * 添加休假设置
	 * 
	 * @param request
	 * @param furloughBean
	 * @return
	 */
	@RequestMapping(value = "/saveFurlough.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object savefurlough(HttpServletRequest request, @RequestBody FurloughBean furloughBean) {
		if (furloughBean != null) {
			if (furloughBean.getFurloughType() == null || furloughBean.getDayNumber() == null) {
				throw new ResultFaultException("0_00001");
			}
			if (furloughService.getFurloughByFurlough(furloughBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (furloughService.saveFurlough(furloughBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 获取休假设置
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getFurloughList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getfurloughList(HttpServletRequest request) {
		return ResultMap.success(furloughService.getFurloughList());
	}

	/**
	 * 修改休假设置
	 * 
	 * @param request
	 * @param furloughBean
	 * @return
	 */
	@RequestMapping(value = "/updateFurlough.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updatefurloughList(HttpServletRequest request, @RequestBody FurloughBean furloughBean) {
		if (furloughBean != null) {
			if (furloughBean.getFurloughType() == null || furloughBean.getDayNumber() == null
					|| furloughBean.getFurloughId() == null || furloughBean.getUpdateTime() == null) {
				throw new ResultFaultException("0_00001");
			}
			Furlough furlough = furloughService.getFurloughListById(furloughBean.getFurloughId());
			if (furlough != null) {
				if (furlough.getUpdateTime().compareTo(furloughBean.getUpdateTime()) == 0) {
					if (furloughService.getFurloughByFurlough(furloughBean)&&furlough.getFurloughType()!=furloughBean.getFurloughType()) {
						throw new ResultFaultException("0_00010");
					} else {
						if (furloughService.updateFurlough(furloughBean)) {
							return ResultMap.success();
						} else {
							throw new ResultFaultException("0_00007");
						}
					}
				} else {
					throw new ResultFaultException("0_00011");
				}
			} else {
				throw new ResultFaultException("0_00012");
			}
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 删除休假设置
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteFurlough.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deletefurlough(HttpServletRequest request) {
		String furloughIdString = request.getParameter("furloughId");
		System.out.println(111111111);
		System.out.println(furloughIdString);
		if (StringUtils.isBlank(furloughIdString)) {
			throw new ResultFaultException("0_00001");
		}
		Integer furloughId = Integer.valueOf(furloughIdString);
		return ResultMap.success(furloughService.deleteFurlough(furloughId));
	}

}
